package io.xxx.omni.osc.sync

import io.xxx.omni.osc.client.StoreClient
import io.xxx.omni.osc.domain.Document
import io.xxx.omni.osc.domain.DocumentMapper
import io.xxx.omni.osc.domain.DocumentType
import io.xxx.omni.osc.domain.PlatformIdentity
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.NoSuchBeanDefinitionException
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.context.ApplicationContext
import org.springframework.kafka.core.KafkaTemplate
import org.springframework.stereotype.Component
import org.springframework.web.bind.annotation.*
import org.springframework.web.client.RestTemplate

@RestController
@RequestMapping("/sync")
class SynchronizerController {

    private val log = LoggerFactory.getLogger(javaClass)

    @Autowired
    private lateinit var applicationContext: ApplicationContext

    @Autowired
    private lateinit var storeClient: StoreClient

    @Autowired
    private lateinit var kafkaTemplate: KafkaTemplate<String, String>

    @Autowired
    private lateinit var documentMapper: DocumentMapper

    @GetMapping("/{platformId}")
    fun sync(
        @PathVariable platformId: String,
        @RequestParam storeId: String,
        @RequestParam documentType: DocumentType?,
        @RequestBody ids: List<String>,
    ) {
        val serviceName = platformId + (documentType?.description ?: "") + Synchronizer::class.java.simpleName
        val service = try {
            applicationContext.getBean(serviceName, Synchronizer::class.java)
        } catch (e: NoSuchBeanDefinitionException) {
            throw IllegalArgumentException("平台[$platformId]不存在")
        }
        val store = storeClient.getOne(storeId) ?: throw IllegalArgumentException("店铺[$storeId]不存在")
        val documents = service.getDocuments(ids)
    }
}

abstract class Synchronizer {

    @Autowired
    protected lateinit var lbRestTemplate: RestTemplate

    abstract fun getDocuments(ids: List<String>): List<Document>
}

@Component
class TbTradeSynchronizer : Synchronizer(), PlatformIdentity.TaoBao {

    override fun getDocuments(ids: List<String>): List<Document> {
        TODO("Not yet implemented")
    }
}

@Component
class TbRefundSynchronizer : Synchronizer(), PlatformIdentity.TaoBao {

    override fun getDocuments(ids: List<String>): List<Document> {
        TODO("Not yet implemented")
    }
}